package model
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	public class Image extends Sprite
	{
		public var loader:Loader;
		private var loader2:Loader;
		private var _url:String;
		public static const COMPLETE:String = "complete";
		public function Image(url:String=null)
		{
			super();						
			if(url != null)
			{	
				_url = url;		
				loadData();
			}
			
		}
		
		private function loadData():void
		{
			if(loader == null){
				loader = new Loader();
				addChild(loader);
				loader.load(new URLRequest(_url));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
				loader.addEventListener(IOErrorEvent.IO_ERROR,onError);
			}else{
				loader2 = new Loader();
				this.addChild(loader2);
				loader2.load(new URLRequest(_url));
				loader2.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
				loader2.addEventListener(IOErrorEvent.IO_ERROR,onError);
			}
			
		}
		
		protected function onError(event:Event):void
		{
			trace("找不到图片")
		}
		
		
		protected function onComplete(event:Event):void
		{
			
			if(loader2){
				
				this.removeChild(loader);
				loader = loader2;
				loader2 = null;	
				
			}
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			
			dispatchEvent(new Event(Image.COMPLETE))	
			
		}
		
		public function get url():String
		{
			return _url;
		}			
		
		public function set url(value:String):void
		{								
				_url = value;
				loadData();							
		
		}

	}
}